package flc.ast.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.activity.AlbumManageActivity;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.Album;
import flc.ast.bean.AlbumBean;
import flc.ast.dataBase.AlbumRepository;
import flc.ast.dataBase.AppDatabase;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.AddAlbumDialog;
import flc.ast.dialog.EditDialog;
import flc.ast.dialog.RenameDialog;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<AlbumBean> listShow;
    private AlbumAdapter albumAdapter;
    private BasePopupView editPopup;
    private int selPosition;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.ivAddAlbum.setOnClickListener(this);
        mDataBinding.ivAlbumManage.setOnClickListener(this);

        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 2));
        albumAdapter = new AlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.addChildClickViewIds(R.id.ivAlbumItemEdit);
        albumAdapter.setOnItemClickListener(this);
        albumAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivAddAlbum) {//新建相册
            showAddDialog();
        } else if (id == R.id.ivAlbumManage) {//相册管理
            startActivity(AlbumManageActivity.class);
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        AlbumActivity.album = albumAdapter.getItem(position).getAlbum();
        startActivity(AlbumActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivAlbumItemEdit) {
            selPosition = position;
            showEditDialog(view);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow = new AlbumRepository(mContext).getAllAlbumsWithImages();
        if (listShow != null && listShow.size() != 0) {
            albumAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.tvAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.tvAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showAddDialog() {
        AddAlbumDialog dialog = new AddAlbumDialog(mContext);
        dialog.setListener(new AddAlbumDialog.Listener() {
            @Override
            public void clickSave(String name) {
                Album album = new Album(name);
                AppDatabase.getInstance(mContext).albumDao().insertAlbum(album);
                getData();
                ToastUtils.showShort(getString(R.string.add_suc));
            }
        });
        dialog.show();
    }

    private void showEditDialog(View view) {
        EditDialog editDialog = new EditDialog(mContext);
        editPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(view).hasShadowBg(true) // 去掉半透明背景
                .asCustom(editDialog);
        editPopup.show();
        editDialog.setListener(new EditDialog.Ilistener() {
            @Override
            public void clickDelete() {
                editPopup.dismiss();
                AppDatabase.getInstance(mContext).albumDao().deleteAlbum(listShow.get(selPosition).getAlbum());
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }

            @Override
            public void clickRename() {
                editPopup.dismiss();
                showRename();
            }
        });
    }

    private void showRename() {
        RenameDialog dialog = new RenameDialog(mContext);
        dialog.setListener(new RenameDialog.Listener() {
            @Override
            public void clickConfirm(String name) {
                Album album = new Album(listShow.get(selPosition).getAlbum().getId(), name);
                AppDatabase.getInstance(mContext).albumDao().updateAlbum(album);
                getData();
            }
        });
        dialog.show();
    }

}